package com.kang.controller;

import com.kang.service.FileApiService;
import com.kang.util.WebResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Api(tags = "文件接口")
@RequestMapping("/oss/file")
@Slf4j
public class FileApiController {

    @Autowired
    private FileApiService fileApiService;

    @ApiOperation("oss图片或者视频文件上传")
    @PostMapping(value = "fileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public WebResponse fileUpload(MultipartFile file) {
        try {
            return WebResponse.resSuccess("oss图片文件上传成功",fileApiService.fileUpload(file));
        } catch (Exception ex) {
            log.info("oss图片文件上传失败",ex);
            return WebResponse.resFail("oss图片文件上传失败",null);
        }
    }


//    @ApiOperation("oss视频文件上传")
//    @PostMapping(value = "videoFileUpload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public WebResponse videoFileUpload(MultipartFile file) {
//        try {
//            return WebResponse.resSuccess("oss视频文件上传成功",fileApiService.videoFileUpload(file));
//        } catch (Exception ex) {
//            log.info("oss视频文件上传失败",ex);
//            return WebResponse.resFail("oss视频文件上传失败",null);
//        }
//    }

    @ApiOperation("oss文件上传")
    @PostMapping(value = "uploadOss",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public WebResponse uploadOss(MultipartFile file) {
        try {
            return WebResponse.resSuccess("oss文件上传成功",fileApiService.uploadOss(file));
        } catch (Exception ex) {
            log.info("oss文件上传失败",ex);
            return WebResponse.resFail("oss文件上传失败",null);
        }
    }

    @ApiOperation("单文件上传文件到本地")
    @PostMapping("fileUploadLocal")
    public WebResponse fileUploadLocal(MultipartFile multiFile) {
        try {
            return WebResponse.resSuccess("上传文件到本地成功",fileApiService.fileUploadLocal(multiFile));
        } catch (Exception ex) {
            log.info("上传文件到本地失败",ex);
            return WebResponse.resFail("上传文件到本地失败",null);
        }
    }

    @ApiOperation("上传多张图片")
    @PostMapping("fileUploadListLocal")
    public WebResponse fileUploadListLocal(@RequestBody MultipartFile[] files) {
        try {
            return WebResponse.resSuccess("上传多张图片成功",fileApiService.fileUploadListLocal(files));
        } catch (Exception ex) {
            log.error("上传多张图片失败",ex);
            return WebResponse.resFail("上传多张图片失败",null);
        }
    }

    @ApiOperation("文件下载到本地")
    @PostMapping("fileDownload")
    public WebResponse fileDownload(HttpServletRequest request, HttpServletResponse response,String fileName) {
        try {
            return WebResponse.resSuccess("文件下载到本地成功",fileApiService.fileDownload(request,response,fileName));
        } catch (Exception ex) {
            log.info("文件下载到本地失败");
            return WebResponse.resFail("文件下载到本地失败",null);
        }
    }




}
